package com.hdoit.pisces.web.home.controllers;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

//import com.alibaba.druid.pool.GetConnectionTimeoutException;
//import com.hdoit.studio.logical.user.service.IUserService;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.SexStatus;
//import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.enums.pub.YesOrNoInteger;
import com.tennetcn.common.logical.superbase.model.Employee;
//import com.tennetcn.common.logical.superbase.model.Role;
import com.tennetcn.common.logical.superbase.model.RoleEmployee;
import com.tennetcn.common.logical.superbase.service.IEmployeeService;
import com.tennetcn.common.logical.superbase.service.IRoleEmployeeService;
import com.tennetcn.common.logical.superbase.service.IRoleService;
import com.tennetcn.common.logical.viewmodel.superbase.EmployeeSearch;
import com.tennetcn.common.utils.ChineseUtils;
//import com.tennetcn.common.utils.ChineseUtils;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DESedeCoder;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.common.utils.MD5Utils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.web.bsex.controllers.pagekits.LoginRegistExController;
import com.tennetcn.web.cbase.AuthPassport;
//import com.tennetcn.web.superbase.controllers.EmployeeController;


@Controller
@RequestMapping(value = "/admin/register/")
public class RegisterController extends LoginRegistExController{
	@Override
	public String getRoutePath() {
		return "areas/admin/register/";
	}
	
	private final String registerInfViewName="registerInf";
	//private final String saveViewName="save";
	
	private final String regSuccessViewName="success";
	
	private IRoleService roleService;
	
	public IRoleService getRoleService(){
		return roleService;
	}
	
	@Resource
	public void setRoleService(IRoleService roleService){
		this.roleService = roleService;
	}
	
	//private IUserService userService;
	private IEmployeeService employeeService;
	
	public IEmployeeService getEmployeeService() {
		return employeeService;
	}

	@Resource
	public void setEmployeeService(IEmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	
	private IRoleEmployeeService roleEmployeeService;
	
	public IRoleEmployeeService getRoleEmployeeService() {
		return roleEmployeeService;
	}

	@Resource
	public void setRoleEmployeeService(IRoleEmployeeService roleEmployeeService) {
		this.roleEmployeeService = roleEmployeeService;
	}
	
	
	@RequestMapping(value = registerInfViewName, method = { RequestMethod.GET})
	@AuthPassport
	public String registerInf(Model model,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		model.addAttribute("employee",new Employee());
		//Role role = new Role();
		//model.addAttribute("role", new Role());
		return view(registerInfViewName);
	}
	
	@RequestMapping(value = registerInfViewName,method = {RequestMethod.POST})
	@AuthPassport
	public String registerInf(Model model,Employee employee,EmployeeSearch search) throws ServletException, IOException{
			ActionResult actionResult;
			String label = "false";
			List<Employee>employees=employeeService.queryList(search);
			for(int i = employees.size()-1; i >=0; i--){
				if(employees.get(i).getEmployeeAccount().equals(employee.getEmployeeAccount())){
					label = "true";
					model.addAttribute("label",label);
					System.out.println(label);
				}
			}
			
//			if(label == "true"){
//				response.getWriter().print(true);
//			}else{
//				response.getWriter().print(false);
//			}
//			
		//	System.out.print("111111");
		    //System.out.println(roleService.queryModelByMark("3"));
//			System.out.print(employee.getEmployeeName());
		     String saveId = CommonUtils.getKeyId();
			 employee.setEmployeeId(saveId);
			 
			 employee.setEmail(employee.getEmail());
			 employee.setSex(SexStatus.unknown);
			 employee.setEmployeeAccount(employee.getEmployeeAccount());
				//employee.setEmployeeCode(employee.getEmployeeCode());
			 employee.setEmployeeName(employee.getEmployeeName());
			 //markcode
			 employee.setMarkCode(ChineseUtils.getPinYinHeadChar(employee.getEmployeeName()));
			 employee.setMobile(employee.getMobile());
				//employee.setSex(employee.getSex());
			 employee.setCreateDate(DateUtils.getCurrent());
			 employee.setDeleteMark(1);
			 employee.setIsLocked(YesOrNoInteger.no);
			 employee.setIsLogin(YesOrNoInteger.yes);
			// String a = employee.getEmployeePass();
			// String md5Pass = MD5Utils.encoderByMd5With32Bit(employee.getEmployeePass());
			// System.out.println(a);
			// System.out.println(md5Pass);
			// employee.setEmployeePass();
			// System.out.println(DESedeCoder.encrypt(md5Pass));
			 employee.setEmployeePass(DESedeCoder.encrypt(MD5Utils.encoderByMd5With32Bit(employee.getEmployeePass())));
			 
			
			RoleEmployee roleEmployee = new RoleEmployee();
		//	roleService.queryStringsByMark("3");
			roleEmployee.setId(CommonUtils.getKeyId());
			

		
			roleEmployee.setRoleId(roleService.queryModelByMark("3").getRoleId());
		//	System.out.println("11111");
		//	role.getRoleId();
			
			roleEmployee.setEmployeeId(employee.getEmployeeId());
			roleEmployee.setModelStatus(ModelStatus.add);
			roleEmployee.setDeleteMark(YesOrNoInteger.no);
			roleEmployee.setCreateDate(employee.getCreateDate());
			
			//roleEmployee.setCreateUserId(employee.getCreateUserId());
			//roleEmployee.setCreateUserName(employee.getCreateUserName());	
			//roleEmployees.add(roleEmployee);
		 //markcode
			// employee.setMarkCode(ChineseUtils.getPinYinHeadChar(employee.getEmployeeName()));
//			employeeService.addModel(employee);
//			roleEmployeeService.addModel(roleEmployee);		
//		PrintWriter out = response.getWriter();
//	    String userAccount = request.getParameter("employeeAccount");
//	    EmployeeDaoImpl employeeDao = new EmployeeDaoImpl();
//	    boolean n = employeeDao.checkAccount(saveId, userAccount);
//	    if(n){
//	    	out.print("true");
//	    }else{
//	    	out.print("false");
//	    }
			employeeService.addModel(employee);
			roleEmployeeService.addModel(roleEmployee);
			
		
		
		if(employeeService.applyChange(employee)){
			actionResult=new ActionResult();
		}else{
			actionResult=new ErrorResult();
		}
//		if(userService.applyChange(employee)){
//			actionResult=new ActionResult();
//		}else{
//			actionResult=new ErrorResult();
//		}
		model.addAttribute("employee", employee);
		model.addAttribute(ManageContainer.RESULTKEY,actionResult);
		
		return view(regSuccessViewName);
	}
	
}
